//
//  LYGuideListViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/5.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYGuideListViewController.h"
#import "LYArticleModel.h"
#import "NSDate+Utils.h"
#import "LYWebViewController.h"

@interface LYGuideListViewController ()<VVYRefreshTableViewDelegate>

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSMutableArray *models;


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation LYGuideListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"艺考指南";
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - 网络请求
/**
 *  加载新数据
 */
- (void)loadNewData{
    
    
    NSString *page = @"1";
    NSString *perpage = @"10";
    
    [LYRequestTool getArticlelistsWithCategoryId:self.model.Id page:page perPpage:perpage success:^(NSArray<LYArticleListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        self.page = 1;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_header endRefreshing];
        
    }];
    
}

/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";
    
    [LYRequestTool getArticlelistsWithCategoryId:self.model.Id page:page perPpage:perpage success:^(NSArray<LYArticleListModel *> *models) {
        
        
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


#pragma mark - tableView
/**
 *  设置tableview行数
 *
 *
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //通过模型数组返回对应行数
    return self.models.count;
    
    
}

/**
 *  设置cell
 *
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"guideList";
    
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //拿到数组模型
    LYArticleListModel *model = self.models[indexPath.row];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.image]placeholderImage:[UIImage imageNamed:@"contacts_default_avatar"]];
    cell.textLabel.text = model.title;
    
    //日期
    NSDate *date = [NSDate dateFromTimeStampString:model.create_time];
    NSString *dateStr = [date stringYearMonthDay];
    cell.detailTextLabel.text = dateStr;
    return cell;
}





/**
 *  选中cell跳转编辑页面
 *
 */

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYArticleListModel *model = self.models[indexPath.row];
    LYWebViewController *webVC = [LYWebViewController webVCWithID:model.Id andType:WebTypeArtical];
    [self.navigationController pushViewController:webVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
}


@end
